﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using System.Data.SQLite;
using System.Collections;
using System.Data;
using System.Data.Common;

namespace Cashier_DAO
{
    public class WarehouseDAO: DAOFactory
    {
        /// <summary>
        /// 查询仓库信息
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="warehouse"></param>
        /// <returns></returns>
        public Warehouse GetWarehouse(DbConnection conn, Warehouse warehouse)
        {
            Warehouse result = null;
            try
            {
                StringBuilder sb = new StringBuilder("select * from warehouse where id>0");
                Hashtable paramtable = new Hashtable();
                this.AddParameters(sb, paramtable, warehouse);

                if (paramtable.Keys.Count > 0)
                {
                    DbCommand cmd = CommandFactory.CreateCommand(sb.ToString(), conn);
                    this.AddCommandParamters(cmd, paramtable);
                    DbDataAdapter da = DataAdapterFactory.CreateDataAdapter(cmd);
                    DataTable dt = new DataTable("warehouse");
                    da.Fill(dt);

                    if (dt != null && dt.Rows.Count == 1)
                    {
                        DataRow row = dt.Rows[0];
                        result = new Warehouse();
                        this.DataRowToModel(row, result);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 查询仓库信息列表
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="warehouse"></param>
        /// <returns></returns>
        public DataTable GetWarehouseTable(DbConnection conn, Warehouse warehouse)
        {
            DataTable dt = null;
            try
            {
                StringBuilder sb = new StringBuilder("select * from warehouse where id>0");
                Hashtable paramtable = new Hashtable();
                this.AddParameters(sb, paramtable, warehouse);

                this.GenerateOrderAndPageSQL(sb, paramtable, warehouse);

                DbCommand cmd = CommandFactory.CreateCommand(sb.ToString(), conn);
                this.AddCommandParamters(cmd, paramtable);
                DbDataAdapter da = DataAdapterFactory.CreateDataAdapter(cmd);
                dt = new DataTable("warehouse");
                da.Fill(dt);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询仓库信息记录数
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="warehouse"></param>
        /// <returns></returns>
        public long GetWarehouseRecord(DbConnection conn, Warehouse warehouse)
        {
            long record = 0;
            try
            {
                StringBuilder sb = new StringBuilder("select * from warehouse where id>0");
                Hashtable paramtable = new Hashtable();
                this.AddParameters(sb, paramtable, warehouse);
                
                DbCommand cmd = CommandFactory.CreateCommand(sb.ToString(), conn);
                this.AddCommandParamters(cmd, paramtable);
                Object obj = cmd.ExecuteScalar();
                if (obj != null)
                    record = (long)obj;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 新增仓库信息
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="warehouse"></param>
        /// <returns></returns>
        public long InsertWarehouse(DbConnection conn, Warehouse warehouse)
        {
            long id = 0;
            try
            {
                id = (long)this.Insert(conn, warehouse);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 更新仓库信息
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="warehouse"></param>
        /// <returns></returns>
        public int UpdateWarehouse(DbConnection conn, Warehouse warehouse)
        {
            int flag = 0;
            try
            {
                flag = this.Update(conn, warehouse);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除仓库信息
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="warehouse"></param>
        /// <returns></returns>
        public int DeleteWarehouse(DbConnection conn, Warehouse warehouse)
        {
            int count = 0;
            try
            {
                count = this.Delete(conn, warehouse);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }
    }
}
